package com.google.gsaguide.fragments;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.google.gsaguide.MainActivity;
import com.google.gsaguide.R;
import com.google.gsaguide.adapters.FAQListAdapter;
import com.google.gsaguide.otherlibs.AnimatedExpandableListView;

/**
 * This class represents the FAQ Fragment.
 * @author Felipe Porge Xavier - http://www.felipeporge.com
 */
public class FAQFragment extends Fragment {

    private AnimatedExpandableListView mExpandableListView;
    private FAQListAdapter mFAQAdapter;
    private Context mContext;
    public int[] positionsToExpand = null;

    /**
     * This method allows to get a new instance of FAQFragment.
     * @return - New FAQFragment instance.
     */
    public static FAQFragment newInstance() {
        FAQFragment fragment = new FAQFragment();
        return fragment;
    }

    /**
     * This method allows to get a new instance of FAQFragment and expanding an
     * item in the ExpandableListView.
     *
     * @param positionToExpand - Position of the item that you need to expand.
     * @return - New FAQFragment instance.
     */
    public static FAQFragment newInstance(int positionToExpand) {
        FAQFragment fragment = new FAQFragment();
        int[] p = {positionToExpand};
        fragment.positionsToExpand = p;
        return fragment;
    }

    /**
     * This method allows to get a new instance of FAQFragment and expanding an
     * item in the ExpandableListView.
     *
     * @param positionsToExpand - Positions of the items that you need to expand.
     * @return - New FAQFragment instance.
     */
    public static FAQFragment newInstance(int[] positionsToExpand) {
        FAQFragment fragment = new FAQFragment();
        fragment.positionsToExpand = positionsToExpand;
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_faq, container, false);
        mContext = getActivity().getApplicationContext();


        // Get the questions and answers;
        String[] questions = getResources().getStringArray(R.array.FAQ_questions);
        String[] answers = getResources().getStringArray(R.array.FAQ_answers);

        // Set the FAQFragment content.
        mFAQAdapter = new FAQListAdapter(mContext);
        mFAQAdapter.setData(questions, answers, null);

        // Get the ExpandableListView and verify if it needs to expand some item.
        mExpandableListView = (AnimatedExpandableListView) view.findViewById(R.id.faqList);
        mExpandableListView.setAdapter(mFAQAdapter);
        if(positionsToExpand != null) {
            for (int p : positionsToExpand)
                mExpandableListView.expandGroupWithAnimation(p);
            positionsToExpand = null;
        }

        // In order to show animations, we need to use a custom click handler
        // for our ExpandableListView.
        mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                // We call collapseGroupWithAnimation(int) and
                // expandGroupWithAnimation(int) to animate group
                // expansion/collapse.
                if (mExpandableListView.isGroupExpanded(groupPosition)) {
                    mExpandableListView.collapseGroupWithAnimation(groupPosition);
                } else {
                    mExpandableListView.expandGroupWithAnimation(groupPosition);
                }
                return true;
            }

        });

        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(MainActivity.FAQ_FRAGMENT);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}
